package cn.jobstart.common;

import java.io.*;

/**
 * @author sunke
 * @Date 2021/3/10 22:54
 * @description
 */

public class Base64FileUtil {


    public static String fileToBase64(File file) {

        String base64 = null;
        InputStream in = null;
        try {

            in = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            in.read(bytes);
            base64 = java.util.Base64.getEncoder().encodeToString(bytes);

            return base64;
            // base64 ="data:image/jpeg;base64,"+base64;
//            base64 ="data:image/png;base64,"+base64;

        } catch (Exception e) {
            //e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        return base64;

    }


    public static String imgToBase64(File file) {
        String base64 = null;
        InputStream in = null;
        try {

            in = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            in.read(bytes);
            base64 = java.util.Base64.getEncoder().encodeToString(bytes);

           // base64 ="data:image/jpeg;base64,"+base64;
            base64 ="data:image/png;base64,"+base64;

        } catch (Exception e) {
           //e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        return base64;
    }

    /**
     * @param path
     * @return String
     * @description 将文件转base64字符串
     * @date 2018年3月20日
     * @author changyl
     * File转成编码成BASE64
     */


    public static String fileToBase64(String path) {
        return fileToBase64(new File(path));
    }


    public static String imgToBase64(String path) {
        return imgToBase64(new File(path));
    }

    //BASE64解码成File文件
    public static void base64ToFile(String destPath, String base64, String fileName) {
        File file = null;
        //创建文件目录
        String filePath = destPath;
        File dir = new File(filePath);
        if (!dir.exists() && !dir.isDirectory()) {
            dir.mkdirs();
        }
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        try {
            byte[] bytes = java.util.Base64.getDecoder().decode(base64);
            file = new File(filePath + "/" + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
